package com.gitee.ice1938.module.admin.app.manage.appdetail;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Order;
import org.springframework.jdbc.core.BaseRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.QueryInfo;
import org.springframework.stereotype.Repository;

/**
 * AppDetailController
 * 
 * 
 * @author ice1938@qq.com 2019-12-2 10:38:57
 *
 */
@Repository
public class AppDetailRepository extends BaseRepository {

	@Resource
	private JdbcTemplate jdbcTemplate;

	private QueryInfo querySql(Map<String, Object> param, Pageable pageable, Order order) {
		String keyword = param.get("q_search_key") == null ? null : param.get("q_search_key").toString().trim();
		Date timeStart = param.get("q_time_start") == null ? null : (Date) param.get("q_time_start");
		Date timeEnd = param.get("q_time_end") == null ? null : (Date) param.get("q_time_end");
		List<Object> params = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder("SELECT ");
		// app_package
		sql.append("ad.app_package,");
		// 拥有者ID
		sql.append("ad.owner_uuid,");
		// 评分
		sql.append("ad.cur_score,");
		// 更新时间
		sql.append("ad.update_time");

		//
		sql.append(" FROM app_detail as ad where 1=1 ");
		// 模糊查询模版
		if (keyword != null && !keyword.isEmpty()) {
			sql.append(" and ad.app_package like ?");
			params.add("%" + keyword + "%");
		}
		// 模糊查询模版
		if (keyword != null && !keyword.isEmpty()) {
			sql.append(" and ad.owner_uuid like ?");
			params.add("%" + keyword + "%");
		}
		/*
		 * 时间查询模版 if (timeStart != null) {
		 * sql.append(" and unix_timestamp(ad.update_time) >= ? ");
		 * params.add(timeStart.getTime() / 1000); }
		 */

		String sqlCount = builderCountSql(sql.toString());
		if (order != null) {
			sql.append(" ORDER BY ");
			sql.append(underscoreName(order.getProperty()));
			sql.append(" ");
			sql.append(order.getDirection());
		}
		if (pageable != null) {
			sql.append(" LIMIT ");
			sql.append(pageable.getPageNumber() * pageable.getPageSize());
			sql.append(", ");
			sql.append(pageable.getPageSize());
		}
		return new QueryInfo(sql.toString(), sqlCount, params);

	}

	public Page<AppDetailEntity> findData(Map<String, Object> param, Pageable pageable, Order order) {
		QueryInfo sqlinfo = querySql(param, pageable, order);

		List<AppDetailEntity> content = jdbcTemplate.query(sqlinfo.getSql(), sqlinfo.getParams().toArray(),
		        BeanPropertyRowMapper.newInstance(AppDetailEntity.class));
		if (pageable != null) {
			long total = jdbcTemplate.queryForObject(sqlinfo.getCountSql(), sqlinfo.getParams().toArray(), Long.class);
			return new PageImpl<AppDetailEntity>(content, pageable, total);
		}
		return new PageImpl<AppDetailEntity>(content);
	}

	public int update(AppDetailEntity data, String userLoginName) {
		StringBuilder sql = new StringBuilder("UPDATE app_detail  SET  ");
		List<Object> params = new ArrayList<>();
		// app_package
		sql.append("app_package = ?,");
		params.add(data.getAppPackage());
		// 拥有者ID
		sql.append("owner_uuid = ?,");
		params.add(data.getOwnerUuid());
		// 评分
		sql.append("cur_score = ?,");
		params.add(data.getCurScore());
		// 更新时间
		sql.append("update_time = now()");

		sql.append(" WHERE ");
		// app_package
		sql.append("app_package = ?");
		params.add(data.getAppPackage());
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int insert(AppDetailEntity data, String userLoginName) {

		StringBuilder sql = new StringBuilder("INSERT INTO app_detail ( ");
		// app_package
		sql.append("app_package ,");
		// 拥有者ID
		sql.append("owner_uuid ,");
		// 评分
		sql.append("cur_score ,");
		// 更新时间
		sql.append("update_time  ) VALUES (?,?,?,now())");
		int update = jdbcTemplate.update(sql.toString(), data.getAppPackage(), data.getOwnerUuid(), data.getCurScore());
		return update;
	}

	public int disable(String appPackage, Boolean disable, String userLoginName) {
		StringBuilder sql = new StringBuilder("UPDATE app_detail  SET  disable =?");
		List<Object> params = new ArrayList<>();
		params.add(disable);
		sql.append(" WHERE ");
		sql.append("app_package = ?");
		params.add(appPackage);
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int delete(List<String> ids) {
		if (ids == null || ids.isEmpty()) {
			return 0;
		}
		StringBuilder sql = new StringBuilder("DELETE FROM app_detail WHERE ");
		List<Object> params = new ArrayList<>();
		sql.append("app_package in (");
		for (int i = 0; i < ids.size(); i++) {
			String id = ids.get(i);
			if (i == 0) {
				sql.append("?");
			} else {
				sql.append(",?");
			}
			params.add(id);
		}
		sql.append(")");
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}
}
